
package com.sec.android.app.myfiles.module.shortcut;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsProviderImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.foldertree.FolderTreeDbTableInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import java.io.File;

import static com.sec.android.app.myfiles.module.abstraction.FileRecord.ShortcutType.HomeScreen;
import static com.sec.android.app.myfiles.module.abstraction.FileRecord.ShortcutType.MyFiles;

public class ShortcutProviderImp extends AbsProviderImp {

    private static final int DB_VER_COL_ADDED_ADD_FILE_TYPE = 301;

    public ShortcutProviderImp(Context context) {
        super(context);
    }

    @Override
    protected FileRecord.StorageType getStorage() {
        return FileRecord.StorageType.Shortcut;
    }

    @Override
    protected boolean needRefresh(Uri uri, FileRecord curRecord) {
        return true;
    }

    @Override
    protected void _refreshDB(Uri uri, FileRecord curRecord) {
        try (Cursor cursor = super._query(uri, null, null, null, null)) {
            if (cursor == null || cursor.getCount() <= 0 || !cursor.moveToFirst()) {
                return;
            }
            boolean refreshed = false;
            do {
                if (checkValid(cursor)) {
                    refreshed = true;
                }
            } while (cursor.moveToNext());

            if (refreshed) {
                if (AppFeatures.isTabletUIMode()) {
                    DbTableInfo FolderTreeTableInfo = FolderTreeDbTableInfo.getInstance();
                    notifyChange(Uri.parse(FolderTreeTableInfo.getUri()));
                }
                notifyChange(uri);
            }
        }
    }

    private boolean checkValid(Cursor cursor) {
        boolean bRet = false;
        final int pathIndex = cursor.getColumnIndex(mDbTableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH));
        final int shortcutTypeIndex = cursor.getColumnIndex(mDbTableInfo.getColumnName(DbTableInfo.COLUMN_ID.SHORTCUT_TYPE));
        final int fileTypeIndex = cursor.getColumnIndex(mDbTableInfo.getColumnName(DbTableInfo.COLUMN_ID.FILE_TYPE));
        final int mimeTypeIndex = cursor.getColumnIndex(mDbTableInfo.getColumnName(DbTableInfo.COLUMN_ID.MIME_TYPE));

        String path = cursor.getString(pathIndex);
        int shortcutType = cursor.getInt(shortcutTypeIndex);
        int fileType = cursor.getInt(fileTypeIndex);
        String mimeType = cursor.getString(mimeTypeIndex);

        if (path == null || path.isEmpty()) {
            return bRet;
        }
        File file = SemFwWrapper.file(path);
        if (!file.exists()) {
            removeItem(path, FileRecord.ShortcutType.values()[shortcutType], fileType, mimeType);
            bRet = true;
        }
        return bRet;
    }

    private void removeItem(String path, FileRecord.ShortcutType shortcutType, int fileType, String mimeType) {
        String selection = mDbTableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH) + " =?  AND " + mDbTableInfo.getColumnName(DbTableInfo.COLUMN_ID.SHORTCUT_TYPE) + " = " + shortcutType.ordinal();
        String[] selectionArgs = new String[]{path};
        _delete(selection, selectionArgs);

        if (shortcutType == HomeScreen) {
            FileRecord shortcutRecord = FileRecord.createFileRecord(FileRecord.StorageType.Shortcut, path);
            shortcutRecord.setFileType(fileType);
            shortcutRecord.setMimeType(mimeType);
            ShortcutUtils.removeHomeScreenShortcut(mContext, shortcutRecord);
        } else if (shortcutType == MyFiles) {
            PreferenceUtils.setMyFilesShortcutCount(mContext, -1);
        }
    }

    @Override
    protected Cursor _query(Uri uri, String[] projectionIn, String selection, String[]
            selectionArgs, String sortOrder) {

        _refreshDB(uri, null);
        return super._query(uri, projectionIn, selection, selectionArgs, sortOrder);
    }

    @Override
    protected void updateTable(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(this, "oldVersion " + oldVersion + " newVersion " + newVersion);
        if (oldVersion < DB_VER_COL_ADDED_ADD_FILE_TYPE) {
            super.updateTable(db, oldVersion, newVersion);
        }
    }
}
